package com.enjoyor.soa.traffic.restt.tpcms.dubbo.invoke;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.ubms.dto.DeptInfoDto;
import com.enjoyor.soa.traffic.api.ubms.dto.OffsiteInfoDto;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboDeptInfoService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboOffsiteInfoService;
import com.enjoyor.soa.traffic.restt.tpcms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.helper.JsonHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;
import com.enjoyor.soa.traffic.util.pojo.ZtreeNode;

@Component
public class UbmsDeptBayonetServiceInvoke {
    /**
     * 部门卡口树Tree
     */
    public static List<TreeNode> bayonetDeptTree;

    /**
     * 部门卡口树Ztree
     */
    public static List<ZtreeNode> bayonetDeptZTree;

    @Reference(timeout = 10000)
    private DubboDeptInfoService deptInfoService;

    @Reference(timeout = 10000)
    private DubboOffsiteInfoService offsiteInfoService;

    @Value("${root.dept}")
    private String root_dept;

    @SuppressWarnings({ "unchecked", "static-access" })
    public List<TreeNode> getDeptBayonetTree() {
        try {
            List<TreeNode> treeListDept = new ArrayList<TreeNode>();
            ResultPojo rootDeptpojo = deptInfoService.getDeptInfoById(root_dept);
            if (rootDeptpojo.getAppCode().compareTo("0") == 0 && rootDeptpojo.getResultList() != null) {
                List<DeptInfoDto> rootDeptInfoDto = (List<DeptInfoDto>) rootDeptpojo.getResultList();
                TreeNode treeDept1 = new TreeNode();
                treeDept1.setId(root_dept);
                treeDept1.setText(rootDeptInfoDto.get(0).getDeptName());
                treeDept1.setState("open");
                treeDept1.setChecked(false);
                treeDept1.setIcon(null);
                List<TreeNode> treeChildren1 = new ArrayList<TreeNode>();
                ResultPojo bayonetPojo = offsiteInfoService.getOffsiteInfo(null, null, null);
                List<OffsiteInfoDto> bayonetList = (List<OffsiteInfoDto>) bayonetPojo.getResultList();
                // 先获取根节点部门的卡口点位
                if (bayonetList != null && bayonetList.size() > 0) {
                    for (OffsiteInfoDto offsiteDto : bayonetList) {
                        if (root_dept.equals(offsiteDto.getManageDept())) {
                            TreeNode treeOffsite1 = new TreeNode();
                            treeOffsite1.setId(offsiteDto.getOffsiteId());
                            treeOffsite1.setText(offsiteDto.getOffsiteName());
                            treeOffsite1.setState("close");
                            treeOffsite1.setChecked(false);
                            treeOffsite1.setIcon(null);
                            treeChildren1.add(treeOffsite1);
                        }
                    }
                }
                // 再获取根节点下级部门的卡口点位
                ResultPojo DeptGroupPojo = deptInfoService.getChildDeptInfoById(root_dept);
                List<DeptInfoDto> deptGroupList = (List<DeptInfoDto>) DeptGroupPojo.getResultList();
                if (deptGroupList != null && deptGroupList.size() > 0) {
                    for (DeptInfoDto deptInfoDto : deptGroupList) {
                        if (root_dept.equals(deptInfoDto.getDeptId()) || !"DEPTPROPER01".equals(deptInfoDto.getDeptProperCode())) {
                            continue;
                        }
                        TreeNode treeDept2 = new TreeNode();
                        treeDept2.setId(deptInfoDto.getDeptId());
                        treeDept2.setText(deptInfoDto.getDeptName());
                        treeDept2.setState("open");
                        treeDept2.setChecked(false);
                        treeDept2.setIcon(null);
                        List<TreeNode> treeChildren2 = new ArrayList<TreeNode>();
                        if (bayonetList != null && bayonetList.size() > 0) {
                            for (OffsiteInfoDto offsiteDto : bayonetList) {
                                if (deptInfoDto.getDeptId().equals(offsiteDto.getManageDept())) {
                                    TreeNode treeOffsite2 = new TreeNode();
                                    treeOffsite2.setId(offsiteDto.getOffsiteId());
                                    treeOffsite2.setText(offsiteDto.getOffsiteName());
                                    treeOffsite2.setState("close");
                                    treeOffsite2.setChecked(false);
                                    treeOffsite2.setIcon(null);
                                    treeChildren2.add(treeOffsite2);
                                }
                            }
                        }
                        treeDept2.setChildren(treeChildren2);
                        treeChildren1.add(treeDept2);
                    }
                }

                treeDept1.setChildren(treeChildren1);
                treeListDept.add(treeDept1);
            }
            this.bayonetDeptTree = treeListDept;
            return treeListDept;
        } catch (Exception e) {
            throw e;
        }
    }

    @SuppressWarnings({ "unchecked", "static-access" })
    public List<ZtreeNode> getDeptBayonetZTree() {
        try {
        	List<ZtreeNode> ZtreelistDept = new ArrayList<ZtreeNode>();
        	ResultPojo rootDeptpojo = deptInfoService.getDeptInfoById(root_dept);
            if (rootDeptpojo.getAppCode().compareTo("0") == 0 && rootDeptpojo.getResultList() != null) {
                List<DeptInfoDto> rootDeptInfoDto = (List<DeptInfoDto>) rootDeptpojo.getResultList();
                ZtreeNode node = new ZtreeNode();
                node.setOpen(true);
                node.setId(root_dept);
                node.setPId(rootDeptInfoDto.get(0).getParentId());
                node.setName(rootDeptInfoDto.get(0).getDeptName());
                node.setType("部门");
                node.setIsParent("true");
                node.setParam(JsonHelper.beanToJsonStr(rootDeptInfoDto.get(0)));
                ZtreelistDept.add(node);
            }
        	
            ResultPojo DeptGroupPojo = deptInfoService.getChildDeptInfoById(root_dept);
            List<DeptInfoDto> deptGroupList = (List<DeptInfoDto>) DeptGroupPojo.getResultList();
            if (deptGroupList != null && deptGroupList.size() > 0) {
                for (DeptInfoDto deptInfoDto : deptGroupList) {
                	if (!"DEPTPROPER01".equals(deptInfoDto.getDeptProperCode())) {
                        continue;
                    }
                    ZtreeNode node = new ZtreeNode();
                    if (deptInfoDto.getDeptId().equals(root_dept)) {
                        node.setOpen(true);
                    } else {
                        node.setOpen(false);
                    }
                    node.setId(deptInfoDto.getDeptId());
                    node.setPId(deptInfoDto.getParentId());
                    node.setName(deptInfoDto.getDeptName());
                    node.setType("部门");
                    node.setIsParent("true");
                    node.setParam(JsonHelper.beanToJsonStr(deptInfoDto));
                    ZtreelistDept.add(node);
                }
            }
            ResultPojo bayonetPojo = offsiteInfoService.getOffsiteInfo(null, null, null);
            List<OffsiteInfoDto> bayonetList = (List<OffsiteInfoDto>) bayonetPojo.getResultList();
            List<ZtreeNode> ztreeList = new ArrayList<ZtreeNode>();
            if (bayonetList != null && bayonetList.size() > 0) {
                for (ZtreeNode deptNode : ZtreelistDept) {
                    ztreeList.add(deptNode);
                    for (OffsiteInfoDto dto : bayonetList) {
                        // 树图加载卡口点位
                        if (deptNode.getId() != null && !deptNode.getId().equals("") && dto.getManageDept() != null
                                && !dto.getManageDept().equals("")) {
                            if (deptNode.getId().equals(dto.getManageDept())) {
                                ZtreeNode node = new ZtreeNode();
                                node.setId(dto.getOffsiteId());
                                node.setPId(deptNode.getId());
                                node.setName(dto.getOffsiteName());
                                node.setOpen(false);
                                node.setType("卡口");
                                node.setIsParent("false");
                                node.setParam(JsonHelper.beanToJsonStr(dto));
                                ztreeList.add(node);
                            }
                        }

                    }
                }
            }
            this.bayonetDeptZTree = ztreeList;
            return ztreeList;
        } catch (Exception e) {
            throw e;
        }
    }
    
    @SuppressWarnings("unchecked")
	public List<String> getDeptsByParentId(String deptId) {
		List<String> deptIds = new ArrayList<String>();
		if (deptId != null && !"".equals(deptId)) {
			deptIds.add(deptId);
			ResultPojo deptPojo = deptInfoService.getDeptInfoByParentId(deptId);
			if (CheckPojoHelper.isValid(deptPojo)) {
				List<DeptInfoDto> list = (List<DeptInfoDto>) deptPojo.getResultList();
				for (DeptInfoDto deptInfoDto : list) {
					// 直属下辖业务部门
					//if (deptInfoDto.getDeptProperCode().compareTo("DEPTPROPER01") == 0) {
						deptIds.add(deptInfoDto.getDeptId());
					//}
				}
			}
		}
		return deptIds;
	}

}
